<!DOCTYPE html>
<html lang="zh-cmn-Hans">
<head>
<meta charset="utf-8" />
<title>E:nth-child(n)</title>
<meta name="description" content="CSS3参考手册之: E:nth-child(n)" />
<meta name="keywords" content="E:nth-child(n), css3选择符, css3, css3参考手册" />
<meta name="author" content="Joy Du(飘零雾雨), dooyoe@gmail.com, www.doyoe.com" />
<meta name="robots" content="all" />
<!--[if lte IE 8]>
<script src="../../js/html5.js"></script>
<![endif]-->
<link rel="stylesheet" href="../../skin/article.css" />
</head>
<body>
<nav id="guide" class="g-mod">
	<div class="from">
		<a href="http://css.doyoe.com/" target="_blank">CSS3参考手册</a>
		<span>&#187;</span>
		<a href="../index.htm">选择符列表</a>
		<span>&#187;</span>
		<a href="index.htm" id="category" rel="selectors/pseudo-classes" name="nth-child(n)">伪类选择符</a>
		<span>&#187;</span>
	</div>
	<div class="to">
		<span class="label">相关内容：</span>
		<div class="g-combobox g-transition">
			<a href="?" class="g-transition target">
				<strong>其它伪类选择符</strong>
				<span>选择其它项<!--[if lte IE 7]><ins>IE7 and earlier, Get to die</ins><![endif]--></span>
			</a>
			<div class="g-transition list">
				<ul>
					<!-- 插入分类快速导航 -->
				</ul>
			</div>
		</div>
	</div>
</nav>
<header id="hd">
	<section id="title" class="g-mod">
		<h1 class="tit">E:nth-child(n)</h1>
		<ul class="info">
			<li><strong>版本：CSS3</strong></li>
		</ul>
		<!-- 插入浏览器信息 -->
	</section>
</header>
<section id="bd">
	<section id="syntax" class="g-mod g-attr">
		<h2 class="tit">语法：</h2>
		<div class="cont">
			<p><strong>E:nth-child(n)</strong> { sRules }</p>
		</div>
	</section>
	<section id="intro" class="g-mod g-attr">
		<h2 class="tit">说明：</h2>
		<div class="cont">
			<strong>匹配父元素的第n个子元素E，假设该子元素不是E，则选择符无效。</strong>
			<ul>
				<li>要使该属性生效，E元素必须是某个元素的子元素，E的父元素最高是body，即E可以是body的子元素</li>
				<li>该选择符允许使用一个乘法因子(n)来作为换算方式，比如我们想选中所有的偶数子元素E，那么选择符可以写成：E:nth-child(2n)</li>
				<li>
					<strong>使用E:nth-child(n)实现奇偶：</strong>
					<div class="gquote">
						<p class="gquote-tit"><strong>示例代码：</strong></p>
						<blockquote class="gquote-cont"><code><xmp><style>
li:nth-child(2n){color:#f00;} /* 偶数 */
li:nth-child(2n+1){color:#000;} /* 奇数 */
</style>

<ul>
	<li>列表项一</li>
	<li>列表项二</li>
	<li>列表项三</li>
	<li>列表项四</li>
</ul></xmp></code></blockquote>
						<p class="gquote-info">因为(n)代表一个乘法因子，可以是0, 1, 2, 3, ..., 所以(2n)换算出来会是偶数，而(2n+1)换算出来会是奇数</p>
					</div>
				</li>
				<li>
					<p>该选择符允许使用一些关键字，比如：odd, even</p>
					<div class="gquote">
						<p class="gquote-tit"><strong>使用odd, even实现奇偶：</strong></p>
						<blockquote class="gquote-cont"><code><xmp><style>
li:nth-child(even){color:#f00;} /* 偶数 */
li:nth-child(odd){color:#000;} /* 奇数 */
</style>

<ul>
	<li>列表项一</li>
	<li>列表项二</li>
	<li>列表项三</li>
	<li>列表项四</li>
</ul></xmp></code></blockquote>
						<p class="gquote-info">关键字odd代表奇数，even代表偶数</p>
					</div>
				</li>
				<li>
					<strong>有一点需要注意的是：</strong>
					<div class="gquote">
						<p class="gquote-tit"><strong>HTML示例代码：</strong></p>
						<blockquote class="gquote-cont"><code><xmp><div>
	<p>第1个p</p>
	<p>第2个p</p>
	<span>第1个span</span>
	<p>第3个p</p>
	<span>第2个span</span>
	<p>第4个p</p>
	<p>第5个p</p>
</div></xmp></code></blockquote>
						<p class="gquote-tit"><strong>CSS Case 1：</strong></p>
						<blockquote class="gquote-cont"><code><xmp>p:nth-child(2){color:#f00;}</xmp></code></blockquote>
						<p class="gquote-info">很明显第2个p会被命中然后变成红色</p>
						<p class="gquote-tit"><strong>CSS Case 2：</strong></p>
						<blockquote class="gquote-cont"><code><xmp>p:nth-child(3){color:#f00;}</xmp></code></blockquote>
						<p class="gquote-info">这是会命中第3个p么？如果你这么认为那就错了，这条选择符就不会命中任何一个元素。</p>
						<p class="gquote-tit"><strong>CSS Case 3：</strong></p>
						<blockquote class="gquote-cont"><code><xmp>p:nth-child(4){color:#f00;}</xmp></code></blockquote>
						<p class="gquote-info">这时你以为会命中第4个p，但其实命中的却是第3个p，因为它是第4个子元素</p>
						<p class="gquote-info">E:nth-child(n)会选择父元素的第n个子元素E，如果第n个子元素不是E，则是无效选择符，但n会递增。</p>
						<p class="gquote-tit"><strong>假设不确定第1个子元素是否为E，但是又想命中第1个E，应该这样写：</strong></p>
						<blockquote class="gquote-cont"><code><xmp>p:first-of-type{color:#f00;}</xmp></code></blockquote>
						<p class="gquote-tit"><strong>或者这样写：</strong></p>
						<blockquote class="gquote-cont"><code><xmp>p:nth-of-type(1){color:#f00;}</xmp></code></blockquote>
						<p class="gquote-info">参考 <a href="first-of-type.htm">E:first-of-type</a> 和 <a href="nth-of-type(n).htm">E:nth-of-type(n)</a></p>
					</div>
				</li>
			</ul>
		</div>
	</section>
	<section class="g-mod g-attr" id="compatible">
		<h2 class="tit">兼容性：</h2>
		<div class="cont">
			<ul class="support-type">
				<li><span class="support">浅绿</span> = 支持</li>
				<li><span class="unsupport">红色</span> = 不支持</li>
				<li><span class="partsupport">粉色</span> = 部分支持</li>
			</ul>
			<table class="g-data">
				<thead>
					<tr>
						<th>IE</th>
						<th>Firefox</th>
						<th>Chrome</th>
						<th>Safari</th>
						<th>Opera</th>
						<th>iOS Safari</th>
						<th>Android Browser</th>
						<th>Android Chrome</th>
					</tr>
				</thead>
				<tbody>
					<tr>
						<td class="unsupport">6.0-8.0</td>
						<td class="support" rowspan="2">2.0+</td>
						<td class="support" rowspan="2">4.0+</td>
						<td class="support" rowspan="2">3.1+</td>
						<td class="support" rowspan="2">3.5+</td>
						<td class="support" rowspan="2">3.2+</td>
						<td class="support" rowspan="2">2.1+</td>
						<td class="support" rowspan="2">18.0+</td>
					</tr>
					<tr>
						<td class="support">IE9.0+</td>
					</tr>
				</tbody>
			</table>
		</div>
	</section>
	<section id="example" class="g-mod g-attr">
		<h2 class="tit">示例：</h2>
		<div class="cont">
			<textarea cols="90" rows="10">
<!DOCTYPE html>
<html lang="zh-cmn-Hans">
<head>
<meta charset="utf-8" />
<title>结构性伪类选择符 E:nth-child(n)_CSS参考手册_web前端开发参考手册系列</title>
<meta name="author" content="Joy Du(飘零雾雨), dooyoe@gmail.com, www.doyoe.com" />
<style>
h1 {
	font-size: 16px;
}
li:nth-child(2) {
	color: #f00;
}
</style>
</head>
<body>
<h1>第二行要变成红色 <code>li:nth-child(2){color:#f00;}</code></h1>
<ul>
	<li>结构性伪类选择符 E:nth-child(n)</li>
	<li>结构性伪类选择符 E:nth-child(n)</li>
	<li>结构性伪类选择符 E:nth-child(n)</li>
</ul>
</body>
</html>
			</textarea>
			<p><input type="button" value="运行" class="g-btn g-btn-sure" /></p>
		</div>
	</section>
</section>
<footer id="ft">
	<aside id="rights" class="g-mod">
		<!-- 插入浏览器及版权信息 -->
	</aside>
</footer>
<script src="../../js/jquery.js"></script>
<script src="../../js/inner.js"></script>
</body>
</html>